// pages/doctor/addpatient/addpatient.js
import Message from 'tdesign-miniprogram/message/index';
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    basalInfo: {
      name: null, //姓名
      phone: null, //电话
      ifGenderIndex: null, //性别
      age: null, //年龄
      bw: null, //体重
      bh: null, //身高
      // BMI: null, //BMI
      diaTypeIndex: 0, //糖尿病类型
      smoke: null, //吸烟年限
      ifDrinkIndex: null, //是否饮酒
      di: null, //糖尿病年限
      ifMacroIndex: null, //是否患大血管并发症
      ifMicroIndex: null, //是否患微血管并发症
      hba1c: null, //糖化血红蛋白
      ifHypoglycemiaIndex: null, //是否低血糖
    },
    diaTypeArray: ['1型糖尿病', '2型糖尿病', '妊娠性糖尿病', '其他'],
  },

  onInputName(e) {
    // console.log('name值变更为', e.detail.value)
    var value = e.detail.value;
    var basalInfo = this.data.basalInfo
    basalInfo.name = value;
    this.setData({
      basalInfo: basalInfo // 将用户输入的内容保存到 data 中
    });
  },
  onInputPhone(e) {
    // console.log('phone值变更为', e.detail.value)
    var value = e.detail.value;
    var basalInfo = this.data.basalInfo
    basalInfo.phone = value;
    this.setData({
      basalInfo: basalInfo // 将用户输入的内容保存到 data 中
    });
  },

  selectMale(e) {
    var basalInfo = this.data.basalInfo
    basalInfo.ifGenderIndex = 1
    this.setData({
      basalInfo: basalInfo,
    });
  },

  selectFemale(e) {
    var basalInfo = this.data.basalInfo
    basalInfo.ifGenderIndex = 0
    this.setData({
      basalInfo: basalInfo,
    });
  },

  onInputAge(e) {
    // console.log('age值变更为', e.detail.value)
    var value = e.detail.value;
    var basalInfo = this.data.basalInfo
    basalInfo.age = value;
    this.setData({
      basalInfo: basalInfo // 将用户输入的内容保存到 data 中
    });
  },

  //普通选择器（糖尿病类型）
  diaTypeBindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      ['basalInfo.diaTypeIndex']: e.detail.value
    })
  },

  onInputBw(e) {
    // console.log('bw值变更为', e.detail.value)
    var value = e.detail.value;
    var basalInfo = this.data.basalInfo
    basalInfo.bw = value;
    this.setData({
      basalInfo: basalInfo // 将用户输入的内容保存到 data 中
    });
  },
  onInputBh(e) {
    // console.log('bh值变更为', e.detail.value)
    var value = e.detail.value;
    var basalInfo = this.data.basalInfo
    basalInfo.bh = value;
    this.setData({
      basalInfo: basalInfo // 将用户输入的内容保存到 data 中
    });
  },
  onInputSmoke(e) {
    // console.log('smoke值变更为', e.detail.value)
    var value = e.detail.value;
    var basalInfo = this.data.basalInfo
    basalInfo.smoke = value;
    this.setData({
      basalInfo: basalInfo // 将用户输入的内容保存到 data 中
    });
  },

  selectDrinkYes(e) {
    var basalInfo = this.data.basalInfo
    basalInfo.ifDrinkIndex = 1
    this.setData({
      basalInfo: basalInfo,
    });
  },

  selectDrinkNo(e) {
    var basalInfo = this.data.basalInfo
    basalInfo.ifDrinkIndex = 0
    this.setData({
      basalInfo: basalInfo,
    });
  },

  onInputDI(e) {
    // console.log('illness duration值变更为', e.detail.value)
    var value = e.detail.value;
    var basalInfo = this.data.basalInfo
    basalInfo.di = value;
    this.setData({
      basalInfo: basalInfo // 将用户输入的内容保存到 data 中
    });
  },

  selectMacroYes(e) {
    var basalInfo = this.data.basalInfo
    basalInfo.ifMacroIndex = 1
    this.setData({
      basalInfo: basalInfo,
    });
  },

  selectMacroNo(e) {
    var basalInfo = this.data.basalInfo
    basalInfo.ifMacroIndex = 0
    this.setData({
      basalInfo: basalInfo,
    });
  },

  selectMicroYes(e) {
    var basalInfo = this.data.basalInfo
    basalInfo.ifMicroIndex = 1
    this.setData({
      basalInfo: basalInfo,
    });
  },

  selectMicroNo(e) {
    var basalInfo = this.data.basalInfo
    basalInfo.ifMicroIndex = 0
    this.setData({
      basalInfo: basalInfo,
    });
  },

  onInputHba1c(e) {
    // console.log('hba1c值变更为', e.detail.value)
    var value = e.detail.value;
    var basalInfo = this.data.basalInfo
    basalInfo.hba1c = value;
    this.setData({
      basalInfo: basalInfo // 将用户输入的内容保存到 data 中
    });
  },

  selectHypoglycemiaYes(e) {
    var basalInfo = this.data.basalInfo
    basalInfo.ifHypoglycemiaIndex = 1
    this.setData({
      basalInfo: basalInfo,
    });
  },

  selectHypoglycemiaNo(e) {
    var basalInfo = this.data.basalInfo
    basalInfo.ifHypoglycemiaIndex = 0
    this.setData({
      basalInfo: basalInfo,
    });
  },

  submitInfo(e) {
    var valid = this.checkInfo();
    if (valid == false)
      return;
    const {
      basalInfo
    } = this.data;
    console.log(this.data.diaTypeArray[basalInfo.diaTypeIndex])
    wx.request({
      url: `${app.globalData.protocol}://${app.globalData.host}/bgmp/api/doctor?Action=CreatePatient`,
      method: 'POST',
      header: {
        "Content-Type": "application/json",
        "x-api-key": wx.getStorageSync('token'),

      },
      data: {
        "name": basalInfo.name,
        "phone": basalInfo.phone,
        "gender": basalInfo.ifGenderIndex,
        "age": parseFloat(basalInfo.age),
        "weight": parseFloat(basalInfo.bw),
        "height": parseFloat(basalInfo.bh),
        "smoke": parseFloat(basalInfo.smoke),
        "alcohol": basalInfo.ifDrinkIndex,
        "diabetes": parseFloat(basalInfo.di),
        "macro": basalInfo.ifMacroIndex,
        "micro": basalInfo.ifMicroIndex,
        "hba1c": parseFloat(basalInfo.hba1c),
        "hypoglycemia": basalInfo.ifHypoglycemiaIndex,
        "typeofdiabetes": this.data.diaTypeArray[basalInfo.diaTypeIndex],
      },
      success: (res) => {
        console.log(res);
        if (res.data.code === 200) {
          Message.success({
            context: this,
            offset: [0, 0],
            duration: 2500,
            content: '新建患者成功',
          });
          wx.navigateBack();
        } else {
          Message.error({
            context: this,
            offset: [0, 0],
            duration: 2500,
            content: '新建患者失败',
          });
        }
      },
      fail: (res) => {
        Message.error({
          context: this,
          offset: [0, 0],
          duration: 2500,
          content: '新建患者失败',
        });
      }
    })
  },
  checkInfo() {
    var basalInfo = this.data.basalInfo
    if (basalInfo.name == null) {
      Message.error({
        context: this,
        offset: [0, 0],
        duration: 2500,
        content: '请填写姓名',
      });
      return false;
    } else if (basalInfo.phone == null) {
      Message.error({
        context: this,
        offset: [0, 0],
        duration: 2500,
        content: '请填写电话号码',
      });
      return false;
    } else if (basalInfo.ifGenderIndex == null) {
      Message.error({
        context: this,
        offset: [0, 0],
        duration: 2500,
        content: '请选择性别',
      });
      return false;
    } else if (basalInfo.age == null) {
      Message.error({
        context: this,
        offset: [0, 0],
        duration: 2500,
        content: '请填写年龄',
      });
      return false;
    } else if (isNaN(basalInfo.age) == true) {
      Message.error({
        context: this,
        offset: [0, 0],
        duration: 2500,
        content: '年龄格式错误',
      });
      return false;
    } else if (basalInfo.bw == null) {
      Message.error({
        context: this,
        offset: [0, 0],
        duration: 2500,
        content: '请填写体重',
      });
      return false;
    } else if (isNaN(basalInfo.bw) == true) {
      Message.error({
        context: this,
        offset: [0, 0],
        duration: 2500,
        content: '体重格式错误',
      });
      return false;
    } else if (basalInfo.bh == null) {
      Message.error({
        context: this,
        offset: [0, 0],
        duration: 2500,
        content: '请填写身高',
      });
      return false;
    } else if (isNaN(basalInfo.bh) == true || basalInfo.bh > 3) {
      Message.error({
        context: this,
        offset: [0, 0],
        duration: 2500,
        content: '身高格式错误',
      });
      return false;
    } else if (basalInfo.smoke == null) {
      Message.error({
        context: this,
        offset: [0, 0],
        duration: 2500,
        content: '请填写吸烟年限',
      });
      return false;
    } else if (isNaN(basalInfo.smoke) == true) {
      Message.error({
        context: this,
        offset: [0, 0],
        duration: 2500,
        content: '吸烟年限格式错误',
      });
      return false;
    } else if (basalInfo.ifDrinkIndex == null) {
      Message.error({
        context: this,
        offset: [0, 0],
        duration: 2500,
        content: '请选择是否饮酒',
      });
      return false;
    } else if (basalInfo.di == null) {
      Message.error({
        context: this,
        offset: [0, 0],
        duration: 2500,
        content: '请填写糖尿病患病年限',
      });
      return false;
    } else if (isNaN(basalInfo.di) == true) {
      Message.error({
        context: this,
        offset: [0, 0],
        duration: 2500,
        content: '糖尿病年限格式错误',
      });
      return false;
    } else if (basalInfo.ifMacroIndex == null) {
      Message.error({
        context: this,
        offset: [0, 0],
        duration: 2500,
        content: '请选择是否患大血管并发症',
      });
      return false;
    } else if (basalInfo.ifMicroIndex == null) {
      Message.error({
        context: this,
        offset: [0, 0],
        duration: 2500,
        content: '请选择是否患微血管并发症',
      });
      return false;
    } else if (basalInfo.hba1c == null) {
      Message.error({
        context: this,
        offset: [0, 0],
        duration: 2500,
        content: '请填写血糖蛋白指标',
      });
      return false;
    } else if (isNaN(basalInfo.hba1c) == true) {
      Message.error({
        context: this,
        offset: [0, 0],
        duration: 2500,
        content: '血糖蛋白指标格式错误',
      });
      return false;
    } else if (basalInfo.ifHypoglycemiaIndex == null) {
      Message.error({
        context: this,
        offset: [0, 0],
        duration: 2500,
        content: '请选择是否低血糖',
      });
      return false;
    }
    return true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})